//
//  BusinessMenuViewController.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 15/11/26.
//  Copyright © 2015年 周佳兴. All rights reserved.
//

#import "BusinessMenuViewController.h"
#import "FounctionCollectionCell.h"
#import "ListsViewController.h"
#import "BusinessCell.h"
#import "TabBarOccupyingController1.h"

@implementation BusinessMenuViewController{
    UILabel *labelTitlt;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.cusTabView.hidden = YES;
//    [self createRightNavBar];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [self setTitleLabelText:self.titleString];
    [self createLeftNavBar];
//    [self createRightNavBar];
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tbView = [[UITableView alloc]init];
    [self.view addSubview:self.tbView];
    self.tbView.delegate = self;
    self.tbView.dataSource = self;
    self.tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tbView.backgroundColor = [UIColor clearColor];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(20/2);
        make.left.equalTo(self.view.mas_left);
        make.bottom.equalTo(self.view.mas_bottom);
        make.right.equalTo(self.view.mas_right);
    }];
    self.tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

-(void)createRightNavBar{
    UIButton *leftNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftNavBtn.frame = CGRectMake(0, 0, 37/2, 37/2);
    [leftNavBtn setBackgroundImage:[UIImage imageNamed:@"形状1-18"] forState:UIControlStateNormal];
    [leftNavBtn addTarget:self action:@selector(RightClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:leftNavBtn];
    self.navigationItem.rightBarButtonItem = item;
    
}

-(void)RightClick:(UIButton *)btn{
    NSLog(@"add");
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _secondArr.count ;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ID = @"BUSINESSCELL";
    BusinessCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[BusinessCell alloc]initWithReuseIdentifier:ID];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
//    cell.titleImg.image = [UIImage imageNamed:@"234"];
    NSDictionary *dic =[_secondArr objectAtIndex:indexPath.row];
    
    NSMutableString *strMenuName = [NSMutableString stringWithFormat:@"%@", [dic objectForKey:@"menuName"]];
    if ([strMenuName rangeOfString:@"手机端"].location != NSNotFound) {
        [strMenuName replaceCharactersInRange:[strMenuName rangeOfString:@"手机端"] withString:@""];
    }
    cell.TitleLabel.text = strMenuName;

    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tbView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.selecCell)return;
    
    NSDictionary *dic = [_secondArr objectAtIndex:indexPath.row];

    NSMutableArray *nextArr = [[NSMutableArray alloc]init];
    for (NSDictionary *dica in _allArr) {
        if ([[NSString stringWithFormat:@"%@", [dica valueForKey:@"parent_menuId"]] isEqualToString:[NSString stringWithFormat:@"%@", [dic valueForKey:@"menuId"]]]) {
            [nextArr addObject:dica];
        }
    }

    if(nextArr.count<=0){
        NSString *classListStr = [NSString stringWithFormat:@"%@", [dic valueForKey:@"menuPageUrl"]];
        if ([classListStr rangeOfString:@"tableId="].location!=NSNotFound) {//课程表页面需要特殊跳转
                        TabBarOccupyingController1 *tbOC = [[TabBarOccupyingController1 alloc]init];
            
                        self.cusTabView.hidden = YES;
            
                        tbOC.dataDic = dic;
                        tbOC.fromPop = YES;
                        [self.navigationController pushViewController:tbOC animated:YES];
            return;
        }

    ListsViewController *lv =[[ListsViewController alloc]init];
    lv.pageId = [dic valueForKey:@"pageId"];
    lv.tableId = [dic valueForKey:@"tableId"];

            [self.navigationController pushViewController:lv animated:YES];
    }else{
        
        
        
        BusinessMenuViewController *nv =[[BusinessMenuViewController alloc]init];
        nv.titleString = [dic objectForKey:@"menuName"];
        [self.navigationController pushViewController:nv animated:YES];
        nv.secondArr = [NSMutableArray arrayWithArray:nextArr];
        nv.allArr = [NSMutableArray arrayWithArray:_allArr];
        
//        nv.menu_url = [_dic2 objectForKey:@"timeInterface"];
        nv.cusTabView = self.cusTabView;

    }

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imgViewLine = [[UIImageView alloc]init];
    [view addSubview:imgViewLine];
    
    [imgViewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left);
        make.top.equalTo(view.mas_top);
        make.height.equalTo(@(1));
        make.width.equalTo(view.mas_width);
    }];
    imgViewLine.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
    
    UIImageView *imgView = [[UIImageView alloc]init];
    [view addSubview:imgView];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(24/2);
        make.centerY.equalTo(view.mas_centerY);
        make.height.equalTo(@(40/2));
        make.width.equalTo(@(4/2));
    }];
    
    labelTitlt = [[UILabel alloc]init];
    
    labelTitlt.text = self.titleString;
    [view addSubview:labelTitlt];
    
    [labelTitlt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.mas_left).offset(20/2);
        make.centerY.equalTo(imgView.mas_centerY);
//        make.height.equalTo(@(40/2));
//        make.width.equalTo(@(4/2));
    }];
    labelTitlt.font = [UIFont systemFontOfSize:30/2];
    labelTitlt.textColor = [UIColor colorWithHex:0x4c4c4c];
    imgView.backgroundColor = [UIColor colorWithHex:0x5da6ff];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 90/2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90/2;
}


#warning this will be deleted
//#pragma mark - collectionView dataSource Or delegate
//-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    return _secondArr.count ;
//}
//-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//{
//    return 1;
//}
//-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString * CellIdentifier = @"UICollectionViewCell";
//    FounctionCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
//    cell.titleImg.image = [UIImage imageNamed:@"234"];
//    NSDictionary *dic =[_secondArr objectAtIndex:indexPath.row];
//    cell.titleLa.text = [dic objectForKey:@"phoneMenuName"];
//    return cell;
//}
//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    ListsViewController *lv =[[ListsViewController alloc]init];
//    [self.navigationController pushViewController:lv animated:YES];
//    lv.dataDic = [_secondArr objectAtIndex:indexPath.row];
//    
//}
//

@end
